package ldkxingzhe.ldkxingzhedemo.menu.slidemenu;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.mikepenz.google_material_typeface_library.GoogleMaterial;
import com.mikepenz.iconics.IconicsDrawable;

import ldkxingzhe.ldkxingzhedemo.R;


/**
 * Created by ke on 15-8-25.
 */
public class DrawerLayoutDemoActivity extends AppCompatActivity{


    private Toolbar toolbar;
    private DrawerLayout drawerLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("TAG", "onCreate");
        setContentView(R.layout.activity_drawerlayout_demo);
        toolbar = (Toolbar)findViewById(R.id.toolbar);
        toolbar.setTitle("网易新闻");
        toolbar.setBackgroundColor(Color.GREEN);
        toolbar.setLogo(new IconicsDrawable(this,
                GoogleMaterial.Icon.gmd_3d_rotation).color(Color.RED).sizeDp(15));
        setSupportActionBar(toolbar);

        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
                R.string.open,R.string.close){
            @Override
            public void onDrawerOpened(View drawerView) {
                Log.d("TAG","onDrawerOpened");
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                Log.d("TAG","onDrawerClosed");
                super.onDrawerClosed(drawerView);
            }
        };

        drawerToggle.syncState();
        drawerLayout.setDrawerListener(drawerToggle);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.drawerlayout_demo, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.item1:
                return toggleDrawerLayout();
            case R.id.item2:
                return toggleDrawerLayout();
            default:
                break;
        }
        return true;
    }

    private boolean toggleDrawerLayout() {
        if(drawerLayout.isDrawerOpen(GravityCompat.START)){
            drawerLayout.closeDrawer(GravityCompat.START);
            return true;
        }

        if(drawerLayout.isDrawerOpen(GravityCompat.END)){
            drawerLayout.closeDrawer(GravityCompat.END);
        }else{
            drawerLayout.openDrawer(GravityCompat.END);
        }
        return true;
    }
}
